#include    "menuscene.h"

#include "../ui/button/button.h"
#include "../ui/input/mouseinputmanager.h"
#include "../ui/layout/singlepicturebackground.h"

#include "../audio/audio_engine.h"


SinglePictureBackground* MenuBackground;

Button* playButton = 0;
Button* creditsButton = 0;
Button* exitButton = 0;



MenuScene::MenuScene(GameApp* app):AppScene(app)
{

    MenuBackground = new SinglePictureBackground("resources/mainmenu.png",800.0f,600.0f);

    //creditsButton exitButton  playButton
    playButton = new Button("resources/playButton.png",75,38);
    playButton->m_function = [this](int a,int b){ this->playButtonClick(a,b); };
    playButton->setPosition(385,270);

    creditsButton = new Button("resources/creditsButton.png",75,38);
    creditsButton->m_function = [this](int a,int b){ this->creditsButtonClick(a,b); };
    creditsButton->setPosition(385,320);

    exitButton = new Button("resources/exitButton.png",75,38);
    exitButton->m_function = [this](int a,int b){ this->exitButtonClick(a,b); };
    exitButton->setPosition(385,450);

    std::string jollybot = "jollybot";
    AudioEngine::getInstance()->playSound(jollybot);

    on();
}

MenuScene::~MenuScene()
{
    SAFE_DELETE_OBJ(MenuBackground);
    SAFE_DELETE_OBJ(playButton);
    SAFE_DELETE_OBJ(creditsButton);
    SAFE_DELETE_OBJ(exitButton);

}

void MenuScene::playButtonClick(int x,int y)
{
    PERSONAL_DEBUG_LOG("playbutton clicked!");

    std::string button = "button";
    AudioEngine::getInstance()->playSound(button);

    off();
    m_app->setGameState(GameApp::GAMESTATE_RUNNING);
}

void MenuScene::creditsButtonClick(int x,int y)
{
    PERSONAL_DEBUG_LOG("creditsbutton clicked!");

    std::string button = "button";
    AudioEngine::getInstance()->playSound(button);

    off();
    m_app->setGameState(GameApp::GAMESTATE_CREDITS);
}

void MenuScene::exitButtonClick(int x,int y)
{
    PERSONAL_DEBUG_LOG("exitbutton clicked!");

    std::string button = "button";
    AudioEngine::getInstance()->playSound(button);

    off();
    m_app->exit();
}


void MenuScene::on()
{
    if(switchOpen == true){return;}

    MouseInputManager* mouseInputManager = MouseInputManager::getInstance();
    mouseInputManager->addClickable(playButton);
    mouseInputManager->addClickable(creditsButton);
    mouseInputManager->addClickable(exitButton);

    Switch::on();
}

void MenuScene::off()
{
    MouseInputManager::getInstance()->removeAllClickable();
    Switch::off();
}

void MenuScene::render()
{
    MenuBackground->render();
    playButton->render();
    creditsButton->render();
    exitButton->render();
}
